package cn.jhz.learn.blog.service.security.resource.article.impl;

import cn.jhz.learn.blog.common.exception.ParamException;
import cn.jhz.learn.blog.component.GLOBAL;
import cn.jhz.learn.blog.component.ValidationHolder;
import cn.jhz.learn.blog.dao.article.BlogArticleSubfieldMapper;
import cn.jhz.learn.blog.po.article.BlogArticleSubfield;
import cn.jhz.learn.blog.po.blogger.BlogBlogger;
import cn.jhz.learn.blog.service.security.resource.article.SubfieldValidationService;
import cn.jhz.learn.blog.service.security.resource.blog.BlogValidationService;

import com.google.common.collect.Lists;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("subfieldValidationService")
public class SubfieldValidationServiceImpl implements SubfieldValidationService {

    private final BlogArticleSubfieldMapper subfieldMapper;
    private final BlogValidationService blogValidationService;

    @Autowired
    public SubfieldValidationServiceImpl(BlogArticleSubfieldMapper subfieldMapper, BlogValidationService blogValidationService) {
        this.subfieldMapper = subfieldMapper;
        this.blogValidationService = blogValidationService;
    }

    @Override
    public void validStrict(Integer uid, Integer id) {
        BlogArticleSubfield subfield =
                (BlogArticleSubfield) ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_ARTICLE_SUBFIELD.ID + id);
        BlogBlogger user = blogValidationService.uidValid(uid);

        if(subfield == null)
            subfield = subfieldMapper.selectByPrimaryKey(id);
        if (subfield == null)
            throw new ParamException("分支不存在!");
        else if (subfield.getBlogId().equals(user.getId())) {
            ValidationHolder.putParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_ARTICLE_SUBFIELD.ID + id, subfield);
            ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_SUBFIELD.VISIBILITY + id,
                    Lists.newArrayList(GLOBAL.RESOURCE.VISIBILITY.ALL));
            ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_SUBFIELD.STATUS + id,
                    Lists.newArrayList(GLOBAL.RESOURCE.STATUS.VALID_ALL));
        } else
            throw new ParamException("未拥有分支");
    }

    @Override
    public void blogValid(String blogName) {
        blogValidationService.valid(blogName);
        ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.SUBFIELDS.VISIBILITY + blogName,
                Lists.newArrayList(GLOBAL.RESOURCE.VISIBILITY.OPEN));
        ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.SUBFIELDS.STATUS + blogName,
                Lists.newArrayList(GLOBAL.RESOURCE.STATUS.VALID));
    }

    @Override
    public void blogValid(Integer uid) {
        BlogBlogger user = blogValidationService.uidValid(uid);
        ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.SUBFIELDS.VISIBILITY + user.getId(),
                Lists.newArrayList(GLOBAL.RESOURCE.VISIBILITY.ALL));
        ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.SUBFIELDS.STATUS + user.getId(),
                Lists.newArrayList(GLOBAL.RESOURCE.STATUS.VALID_ALL));
    }


    @Override
    public void blogValid(Integer uid, String blogName) {
        BlogBlogger blogger = blogValidationService.valid(uid, blogName);
        switch ((Byte)ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.OWN_LEVEL + blogger.getId())){
            case 0:
                ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.SUBFIELDS.VISIBILITY + blogName,
                        Lists.newArrayList(GLOBAL.RESOURCE.VISIBILITY.ALL));
                ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.SUBFIELDS.STATUS + blogName,
                        Lists.newArrayList(GLOBAL.RESOURCE.STATUS.VALID_ALL));
                break;
            case 1:
            case 2:
                ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.SUBFIELDS.VISIBILITY + blogName,
                        Lists.newArrayList(GLOBAL.RESOURCE.VISIBILITY.OPEN));
                ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.SUBFIELDS.STATUS + blogName,
                        Lists.newArrayList(GLOBAL.RESOURCE.STATUS.VALID));
                break;
            default:
                throw new RuntimeException("权限参数异常!:" + ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.OWN_LEVEL + blogger.getId()));
        }
    }

    @Override
    public void valid(Integer uid, Integer id) {
        BlogArticleSubfield subfield = (BlogArticleSubfield) ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_ARTICLE_SUBFIELD.ID + id);
        if(subfield == null){
            subfield = subfieldMapper.selectByPrimaryKey(id);
            if(subfield == null)
                throw new ParamException("分支不存在!");
            ValidationHolder.putParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_ARTICLE_SUBFIELD.ID + id, subfield);
        }
        BlogBlogger blogger = blogValidationService.valid(uid, subfield.getBlogId());
        switch ((Byte)ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.OWN_LEVEL + blogger.getId())){
            case 0:
                ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_SUBFIELD.VISIBILITY + id,
                        Lists.newArrayList(GLOBAL.RESOURCE.VISIBILITY.ALL));
                ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_SUBFIELD.STATUS + id,
                        Lists.newArrayList(GLOBAL.RESOURCE.STATUS.VALID_ALL));
                break;
            case 1:
            case 2:
                if(subfield.getStatus().equals(GLOBAL.RESOURCE.STATUS.VALID)){
                    if(subfield.getVisibility().equals(GLOBAL.RESOURCE.VISIBILITY.OPEN)){
                        ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_SUBFIELD.VISIBILITY + id,
                                Lists.newArrayList(GLOBAL.RESOURCE.VISIBILITY.OPEN));
                        ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_SUBFIELD.STATUS + id,
                                Lists.newArrayList(GLOBAL.RESOURCE.STATUS.VALID));
                    }else
                        throw new ParamException("分支被隐藏!");
                }else
                    throw new ParamException("分支不存在!");
                break;
            default:
                throw new RuntimeException("权限参数异常!:" + ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.OWN_LEVEL + blogger.getId()));
        }
    }

    @Override
    public void valid(Integer id) {
        BlogArticleSubfield subfield = (BlogArticleSubfield) ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_ARTICLE_SUBFIELD.ID + id);
        if(subfield == null){
            subfield = subfieldMapper.selectByPrimaryKey(id);
            if(subfield == null)
                throw new ParamException("分支不存在!");
            else if(!subfield.getStatus().equals(GLOBAL.RESOURCE.STATUS.VALID))
                throw new ParamException("分支不存在!");
            ValidationHolder.putParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_ARTICLE_SUBFIELD.ID + id, subfield);
        }
        BlogBlogger blogger = blogValidationService.valid(subfield.getBlogId());
        if (ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.OWN_LEVEL + blogger.getId()).equals(GLOBAL.RESOURCE.OWN_LEVEL.PUBLIC)){
            if(subfield.getVisibility().equals(GLOBAL.RESOURCE.VISIBILITY.OPEN)){
                ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_SUBFIELD.VISIBILITY + id,
                        Lists.newArrayList(GLOBAL.RESOURCE.VISIBILITY.OPEN));
                ValidationHolder.putLevel(GLOBAL.VALIDATION_KEY.OWN_ENTITY_SUBFIELD.STATUS + id,
                        Lists.newArrayList(GLOBAL.RESOURCE.STATUS.VALID));
            }else
                throw new ParamException("分支被隐藏!");
        } else
            throw new RuntimeException("权限参数异常!:" + ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.OWN_ENTITY_BLOG.OWN_LEVEL + blogger.getId()));
    }
}
